package com.dule.Medic.service_hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.dule.Medic.beans.PrescriptionBean;
import com.dule.Medic.model.MedEntry;
import com.dule.Medic.model.Prescription;
import com.dule.Medic.service.PrescriptionService;

public class PrescriptionHibernateService implements PrescriptionService {

	private final Session session;
	
	public PrescriptionHibernateService(Session session) {
		super();
		this.session = session;
	}

	public Long save(Prescription prescription) 
	{
		Long id = (Long) session.save(prescription);
		return  id;
	}

	public boolean update(Prescription prescription) 
	{
		session.merge(prescription);
		return true;
	}

	public boolean delete(Prescription prescription) {
		session.delete(prescription);
		return true;
	}

	@SuppressWarnings("unchecked")
	public List<Prescription> retrieve(MedEntry medEntry) {
		List<Prescription> prescriptions = session.createCriteria(PrescriptionBean.class).add(Restrictions.eq("medEntry", medEntry)).list();
		return prescriptions;
	}

	public Prescription retrieveOne(Long Id) 
	{
		Criteria crit = session.createCriteria(PrescriptionBean.class).add(Restrictions.eq("id", Id));    
		return (Prescription) crit.uniqueResult();
	}

}
